<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class ajax extends CI_Controller{
      function __construct() {
          parent::__construct();
          $this->load->library('string_lib');
          $this->load->model('ajax_model','ajax');
          $this->load->helper('img_helper');
      }
      function orderlast(){
          $data['list'] = $this->ajax->get_last_order();
          $this->load->view('ajax/orderlast',$data);
      }    
      
      function transfer_notice_last(){
          $list = $this->ajax->get_transfer_notice_last();
          $data['list'] = '
            <table class="admindata">
                <thead>
                    <th>Mã ĐH</th>
                    <th>Tài khoản</th>
                    <th>Xem</th>
                </thead>    
                     
          ';
          $k=1;
          foreach($list as $rs):
          $data['list'] .='
            <tr class="row'.$k.'">
                <td><a href="'.site_url('chonmua/chitietdonhang/'.$rs->CartID).'">'.$rs->CartID.'</a></td>
                <td>'.$rs->LoginName.'</td>
                <td><a href="'.site_url('chonmua/notice_detail/'.$rs->TransferID).'">Xem</td>
            </tr>
          ';
          $k=1-$k;
          endforeach;
         $data['list'] .='</table> '; 
          echo json_encode($data);
          
      }                             
      function thongke(){
          $this->load->view('ajax/thongke');
      }
      
      function publish(){
          $status = $this->input->post('status');
          $id = $this->input->post('id');
          $field = $this->input->post('field');
          $table = $this->input->post('table');
          if($status==0){
              $pub = 1;
          }else{
              $pub = 0;
          }
          $this->ajax->setActiveDeactive($table,$field,$id,$pub);
          echo icon_active("'$table'","'$field'",$id,$pub);
      }
	   function approve(){
          $status = $this->input->post('status');
          $id = $this->input->post('id');
          $field = $this->input->post('field');
          $table = $this->input->post('table');
          if($status==0){
              $pub = 1;
          }else{
              $pub = 0;
          }
		  $admin_approve_id = $this->session->userdata('admin_id');
          $this->ajax->setApproveNotApprove($table,$field,$id,$admin_approve_id,$pub);
          echo icon_approve("'$table'","'$field'",$id,$pub);
      }
      function getalias(){
          $name = $this->input->post('name');
          echo strtolower($this->string_library->stringchange($name));
      }
      
      function uploader(){
            $ProductID = $this->uri->segment(3);
            $session_info = $this->session->userdata('session_id');
            $dir = '../uploads/images/';
            $dir_admin = 'uploads/images/';
            //chmod($uploaddir,0777);
            $size=$_FILES['uploadfile']['size'];
            if($size>204857600)
            {
                    echo "file_biger";
                    unlink($_FILES['uploadfile']['tmp_name']);
                    //exit;
            }            
            $filename = stripslashes($_FILES['uploadfile']['name']);
            $i = strrpos($filename,".");
            if (!$i) { return ""; }
            $l = strlen($filename) - $i;
            $extension = substr($filename,$i+1,$l);                 
            $extension = strtolower($extension); 
            $file_name = str_replace($extension,'',$filename);
            $name = time();
            if($extension =='jpeg'){
                $extension = 'jpg';
            }
            $filename = $dir.$name.'.'.$extension;
            $file_ext = $name.'.'.$extension;
            if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $filename)) {
                $id_img = $this->ajax->add_img($file_ext,$ProductID,$session_info);
                                
                echo $id_img.','.$file_ext;
            } else {
                echo 'error';
            }
      }
  }
?>
